using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using OVS.BusinessObjects;

namespace OVS.DAL
{
	public partial class sysdiagramsDAO
	{
		public sysdiagramsDAO()
		{
			DbProviderHelper.GetConnection();
		}
		public List<sysdiagrams> Getsysdiagramss()
		{
			try
			{
				List<sysdiagrams> lstsysdiagramss = new List<sysdiagrams>();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [name],[principal_id],[diagram_id],[version],[definition] FROM [sysdiagrams]",CommandType.Text);
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					sysdiagrams osysdiagrams = new sysdiagrams();
					osysdiagrams.name = Convert.ToString(oDbDataReader["name"]);
					osysdiagrams.principal_id = Convert.ToInt32(oDbDataReader["principal_id"]);
					osysdiagrams.diagram_id = Convert.ToInt32(oDbDataReader["diagram_id"]);

					if(oDbDataReader["version"] != DBNull.Value)
						osysdiagrams.version = Convert.ToInt32(oDbDataReader["version"]);

					if(oDbDataReader["definition"] != DBNull.Value)
						osysdiagrams.definition = (Byte[])(oDbDataReader["definition"]);
					lstsysdiagramss.Add(osysdiagrams);
				}
				oDbDataReader.Close();
				return lstsysdiagramss;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public sysdiagrams Getsysdiagrams(int diagram_id)
		{
			try
			{
				sysdiagrams osysdiagrams = new sysdiagrams();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [name],[principal_id],[diagram_id],[version],[definition] FROM [sysdiagrams] WHERE [diagram_id]=@diagram_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@diagram_id",DbType.Int32,diagram_id));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					osysdiagrams.name = Convert.ToString(oDbDataReader["name"]);
					osysdiagrams.principal_id = Convert.ToInt32(oDbDataReader["principal_id"]);
					osysdiagrams.diagram_id = Convert.ToInt32(oDbDataReader["diagram_id"]);

					if(oDbDataReader["version"] != DBNull.Value)
						osysdiagrams.version = Convert.ToInt32(oDbDataReader["version"]);

					if(oDbDataReader["definition"] != DBNull.Value)
						osysdiagrams.definition = (Byte[])(oDbDataReader["definition"]);
				}
				oDbDataReader.Close();
				return osysdiagrams;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int Addsysdiagrams(sysdiagrams osysdiagrams)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERT INTO [sysdiagrams]([name],[principal_id],[version],[definition])VALUES(@name,@principal_id,@version,@definition);SELECT SCOPE_IDENTITY();",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@name",DbType.String,osysdiagrams.name));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@principal_id",DbType.Int32,osysdiagrams.principal_id));
				if (osysdiagrams.version.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@version",DbType.Int32,osysdiagrams.version));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@version",DbType.Int32,DBNull.Value));
				if (osysdiagrams.definition!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@definition",DbType.Binary,osysdiagrams.definition));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@definition",DbType.Binary,DBNull.Value));

				return Convert.ToInt32(DbProviderHelper.ExecuteScalar(oDbCommand));
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int Updatesysdiagrams(sysdiagrams osysdiagrams)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATE [sysdiagrams] SET [name]=@name,[principal_id]=@principal_id,[version]=@version,[definition]=@definition WHERE [diagram_id]=@diagram_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@name",DbType.String,osysdiagrams.name));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@principal_id",DbType.Int32,osysdiagrams.principal_id));
				if (osysdiagrams.version.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@version",DbType.Int32,osysdiagrams.version));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@version",DbType.Int32,DBNull.Value));
				if (osysdiagrams.definition!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@definition",DbType.Binary,osysdiagrams.definition));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@definition",DbType.Binary,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@diagram_id",DbType.Int32,osysdiagrams.diagram_id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int Removesysdiagrams(sysdiagrams osysdiagrams)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [sysdiagrams] WHERE [diagram_id]=@diagram_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@diagram_id",DbType.Int32,osysdiagrams.diagram_id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int Removesysdiagrams(int diagram_id)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [sysdiagrams] WHERE [diagram_id]=@diagram_id",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@diagram_id",DbType.Int32,diagram_id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
